﻿using System;
using System.Net;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;

namespace BallMadness
{
    public static class ScoreManager
    {
        static int playerScore = 0;
        static int highScore = 0;
        static List<Worm> worms;

        static TimeSpan wormDelay = new TimeSpan(0, 0, 7);
        static TimeSpan nextWorm;

        public static Texture2D worm;
        static SpriteFont uifont;

        static SoundEffect success;
        public static SoundEffect slam;
        public static SoundEffect death;

        public static bool gameOver;


        static ScoreManager()
        {
            worms = new List<Worm>();
        }


        public static void setScore(int score)
        {
            playerScore += score;
        }

        public static int getScore()
        {
            return playerScore;
        }

        public static void Reset()
        {
            gameOver = false;
            playerScore = 0;
            worms.Clear();
            highScoreAchieved = false;
        }

        public static void OnNavigateTo(ContentManager manager)
        {
            worm = manager.Load<Texture2D>("worm");
            uifont = manager.Load<SpriteFont>("ui");
            success = manager.Load<SoundEffect>("Success");
            death = manager.Load<SoundEffect>("death");
            slam = manager.Load<SoundEffect>("slam");
        }

        public static void OnUpdate(GameTimerEventArgs e)
        {
            if (!gameOver)
            {
                nextWorm -= e.ElapsedTime;
                if (nextWorm <= TimeSpan.Zero && !ScoreManager.gameOver)
                {
                    nextWorm = wormDelay;
                    worms.Add(new Worm());
                }

                for (int c = worms.Count - 1; c >= 0; c--)
                {
                    worms[c].OnUpdate(e);
                    if (worms[c].isOffScreen())
                    {
                        worms.RemoveAt(c);
                    }
                }
            }

            nextOscillation -= e.ElapsedTime;
            if (nextOscillation <= TimeSpan.Zero)
            {
                nextOscillation = oscillationTime;
                show = !show;
            }
        }

        private static TimeSpan nextOscillation;
        private static TimeSpan oscillationTime = new TimeSpan(0, 0, 0, 0, 800);
        private static bool show;
        private static bool highScoreAchieved = false;

        public static void OnDrawWorms(SpriteBatch sb)
        {
            foreach (Worm w in worms)
                w.OnDraw(sb);
        }

        public static void OnDraw(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(
                uifont,
                "Score: " + playerScore,
                new Vector2(10, 10),
                Color.White);

            if (show || !highScoreAchieved)
                spriteBatch.DrawString(
                    uifont,
                    "Best: " + highScore,
                    new Vector2(10, 50),
                    Color.White);

            if (gameOver)
            {
                spriteBatch.DrawString(
                    uifont,
                    "GAME OVER",
                    new Vector2(
                        SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width,
                        SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Height) / 2
                        - uifont.MeasureString("GAME OVER") / 2,
                    Color.White);
                spriteBatch.DrawString(
                    uifont,
                    "Press Back",
                    new Vector2(
                        SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width,
                        SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Height) / 2
                        - uifont.MeasureString("Press Back") / 2
                        + new Vector2(0, 60),
                    Color.White);

                if (playerScore > highScore)
                {
                    highScoreAchieved = true;
                    highScore = playerScore;
                }
            }
        }

        public static void CheckCollision(int radius, Vector2 position)
        {
            for (int c = worms.Count - 1; c >= 0; c--)
            {
                if (worm.Height / 2 + radius > Vector2.Distance(position, worms[c].position))
                {
                    playerScore += Worm.POINTS;
                    worms.RemoveAt(c);
                    success.Play();
                }
            }
        }
    }
}
